package team.seekers.oj.codetest.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.seekers.oj.codetest.dao.CodeTestDescriptionDao;
import team.seekers.oj.codetest.dto.DesCreateReqDTO;
import team.seekers.oj.codetest.dto.DesManageDTO;
import team.seekers.oj.codetest.entity.dao.CodeTestDescriptionDO;
import team.seekers.oj.codetest.entity.dao.CodeTestDescriptionDOField;
import team.seekers.oj.codetest.entity.vo.DesInfoVO;
import team.seekers.oj.common.enums.ApiExceptionEnum;
import team.seekers.oj.common.exception.ApiException;
import team.seekers.oj.common.util.AssertUtils;


@Service
public class CodeTestDescriptionService {

    @Autowired
    private CodeTestDescriptionDao codeTestDescriptionDao;

    public Long desCreate(DesCreateReqDTO reqDTO) {
        CodeTestDescriptionDO descriptionDO = new CodeTestDescriptionDO();
        BeanUtils.copyProperties(reqDTO,descriptionDO);
        AssertUtils.isTrue(codeTestDescriptionDao.save(descriptionDO), ApiExceptionEnum.UNKNOWN_ERROR);
        return descriptionDO.getId();
    }

    public void removeDescription(long descriptionId) {
        CodeTestDescriptionDO codeTestDescriptionDO = codeTestDescriptionDao.getById(descriptionId);
        DesManageDTO desManageDTO = new DesManageDTO();
        BeanUtils.copyProperties(codeTestDescriptionDO,desManageDTO);
        desManageDTO.setDeleted(1);
        desUpdate(desManageDTO);
    }

    public void desUpdate(DesManageDTO reqDTO) {
        CodeTestDescriptionDO codeTestDescriptionDO = codeTestDescriptionDao.lambdaQuery().select(
                CodeTestDescriptionDO::getId,
                CodeTestDescriptionDO::getUserId,
                CodeTestDescriptionDO::getVersion
        ).eq(CodeTestDescriptionDO::getId, reqDTO.getId()).one();
        AssertUtils.notNull(codeTestDescriptionDO, ApiExceptionEnum.DESCRIPTION_NOT_FOUND);

        CodeTestDescriptionDO desUpdateDO = new CodeTestDescriptionDO();
        BeanUtils.copyProperties(reqDTO,desUpdateDO);


        desUpdateDO.setVersion(codeTestDescriptionDO.getVersion());
        UpdateWrapper<CodeTestDescriptionDO> updater = new UpdateWrapper<>();
        updater.eq(CodeTestDescriptionDOField.ID, desUpdateDO.getId());
        if (!codeTestDescriptionDao.update(desUpdateDO, updater)) {
            throw new ApiException(ApiExceptionEnum.SERVER_BUSY);
        }
    }

    public DesInfoVO getDesVO(long DesId) {
        CodeTestDescriptionDO codeTestDescriptionDO = codeTestDescriptionDao.getById(DesId);
        //System.out.println(ListToByteUtil.codeTestUsersToUserIdList(codeTestDO.getUsers()));
        AssertUtils.notNull(codeTestDescriptionDO, ApiExceptionEnum.CODETEST_NOT_FOUND);
        DesInfoVO desInfoVO = new DesInfoVO();
        BeanUtils.copyProperties(codeTestDescriptionDO,desInfoVO);
        return desInfoVO;
    }
}
